package me.jzn.alib.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;

import me.jzn.alib.R;
import me.jzn.core.utils.StrUtil;

/**
 * 如果对TextView外侧包装，同时还要把属性加给自己，可以用这个.
 */
public class TextViewAttrUtil {
    public static void applyAttrsToEditText(Context context, @NonNull AttributeSet attrs, EditText et) {
        applyAttrsToTextView(context, attrs, et);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.InitEditText);

        String hint = array.getString(R.styleable.InitEditText_android_hint);
        int flagAutofillImportance = array.getInt(R.styleable.InitEditText_android_importantForAutofill, -1);
        CharSequence[] autoFillHints = array.getTextArray(R.styleable.InitEditText_android_autofillHints);

        array.recycle();

        if (hint != null) {
            et.setHint(hint);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (flagAutofillImportance > -1) {
                et.setImportantForAutofill(flagAutofillImportance);
            }
            if (autoFillHints != null && autoFillHints.length > 0) {
                String[] strArr = StrUtil.toStringArray((Object[]) autoFillHints);
                et.setAutofillHints(strArr);
            }
        }
    }

    public static void applyAttrsToTextView(Context context, @NonNull AttributeSet attrs, TextView tv) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.InitTextView);

        Drawable drawableLeft = array.getDrawable(R.styleable.InitTextView_android_drawableLeft);
        Drawable drawableTop = array.getDrawable(R.styleable.InitTextView_android_drawableTop);
        Drawable drawableRight = array.getDrawable(R.styleable.InitTextView_android_drawableRight);
        Drawable drawableBottom = array.getDrawable(R.styleable.InitTextView_android_drawableBottom);

        Drawable drawableStart = array.getDrawable(R.styleable.InitTextView_android_drawableStart);
        Drawable drawableEnd = array.getDrawable(R.styleable.InitTextView_android_drawableEnd);

        float drawablePadding = array.getDimension(R.styleable.InitTextView_android_drawablePadding, -1);
        ColorStateList drawableTint = array.getColorStateList(R.styleable.InitTextView_android_drawableTint);

        float textSize = array.getDimensionPixelSize(R.styleable.InitTextView_android_textSize, -1);
        ColorStateList textColor = array.getColorStateList(R.styleable.InitTextView_android_textColor);

        CharSequence text = array.getText(R.styleable.InitTextView_android_text);
        int textStyle = array.getInt(R.styleable.InitTextView_android_textStyle, -1);

        array.recycle();

        if (textStyle > 0) {
            tv.setTypeface(null, textStyle);
        }

        if (textColor != null) {
            tv.setTextColor(textColor);
        }

        if (textSize > 0) {
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        _applyDrawables(tv, drawableLeft, drawableTop, drawableRight, drawableBottom, drawableStart, drawableEnd);

        if (drawablePadding > -1) {
            tv.setCompoundDrawablePadding((int) drawablePadding);
        }
        if (drawableTint != null) {
            _setCompoundDrawableTintList(tv, drawableTint);
        }
        if (text != null) {
            tv.setText(text);
//            if (tv instanceof EditText) {
//                //有手机设置后光标在最左
//                ((EditText) tv).setSelection(tv.getText().length());
//            }
        }
    }

    //经测试，真实的textview中如果同时存在，Left优先于Start.
    private static void _applyDrawables(TextView tv, Drawable drawableLeft, Drawable drawableTop, Drawable drawableRight, Drawable drawableBottom, Drawable drawableStart, Drawable drawableEnd) {
        Drawable[] arr = {drawableLeft, drawableTop, drawableRight, drawableBottom};
        if (drawableLeft == null) {
            arr[0] = drawableStart;
        }
        if (drawableRight == null) {
            arr[2] = drawableEnd;
        }
        Drawable[] drawables = tv.getCompoundDrawables();
        for (int i = 0; i < 4; i++) {
            if (arr[i] != null) {
                drawables[i] = arr[i];
            }
        }

        if (drawableLeft != null || drawableRight != null) {
            //左右有任何一个，直接用绝对定位即可
            tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
            return;
        }
        if (Build.VERSION.SDK_INT >= 17) {
            tv.setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
        } else {
            tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
        }
    }

    public static void applyPaddingAttrs(Context context, @NonNull AttributeSet attrs, View tv) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Padding);
        int padding = (int) array.getDimension(R.styleable.Padding_android_padding, -1);
        int paddingH = (int) array.getDimension(R.styleable.Padding_android_paddingHorizontal, -1);
        int paddingV = (int) array.getDimension(R.styleable.Padding_android_paddingVertical, -1);

        int paddingLeft = (int) array.getDimension(R.styleable.Padding_android_paddingLeft, -1);
        int paddingTop = (int) array.getDimension(R.styleable.Padding_android_paddingTop, -1);
        int paddingRight = (int) array.getDimension(R.styleable.Padding_android_paddingRight, -1);
        int paddingBottom = (int) array.getDimension(R.styleable.Padding_android_paddingBottom, -1);
        int paddingStart = (int) array.getDimension(R.styleable.Padding_android_paddingStart, -1);
        int paddingEnd = (int) array.getDimension(R.styleable.Padding_android_paddingEnd, -1);
        array.recycle();

        int[] paddingArr = {paddingLeft, paddingTop, paddingRight, paddingBottom};

        if (paddingLeft < 0) {
            paddingArr[0] = paddingStart;
        }
        if (paddingRight < 0) {
            paddingArr[3] = paddingEnd;
        }
        ////
        if (paddingArr[0] < 0) {
            paddingArr[0] = paddingH;
        }
        if (paddingArr[2] < 0) {
            paddingArr[2] = paddingH;
        }
        if (paddingArr[1] < 0) {
            paddingArr[1] = paddingV;
        }
        if (paddingArr[3] < 0) {
            paddingArr[3] = paddingV;
        }
        for (int i = 0; i < paddingArr.length; i++) {
            if (paddingArr[i] < 0) {
                paddingArr[i] = padding;
            }
        }
        int[] origPadding = {tv.getPaddingLeft(), tv.getPaddingTop(), tv.getPaddingRight(), tv.getPaddingBottom()};
        for (int i = 0; i < paddingArr.length; i++) {
            if (paddingArr[i] > -1) {
                origPadding[i] = paddingArr[i];
            }
        }
        ////
        if (paddingLeft > -1 || paddingRight > -1) {
            //左右有任何一个，直接用绝对定位即可
            tv.setPadding(origPadding[0], origPadding[1], origPadding[2], origPadding[3]);
            return;
        }
        if (Build.VERSION.SDK_INT >= 17) {
            tv.setPaddingRelative(origPadding[0], origPadding[1], origPadding[2], origPadding[3]);
        } else {
            tv.setPadding(origPadding[0], origPadding[1], origPadding[2], origPadding[3]);
        }
    }

    private static void _setCompoundDrawableTintList(TextView tv, @Nullable ColorStateList tint) {
        if (Build.VERSION.SDK_INT < 23) {
            Drawable[] drawables = tv.getCompoundDrawables();
            Drawable drawableStart = drawables[0];
            if (drawableStart != null) {
                Drawable wrap = DrawableCompat.wrap(drawableStart);
                DrawableCompat.setTintList(wrap, tint);
                DrawableCompat.setTintMode(wrap, PorterDuff.Mode.SRC_IN);
                wrap = wrap.mutate();

                drawableStart = wrap;
            }
            if (Build.VERSION.SDK_INT >= 17) {
                tv.setCompoundDrawablesRelative(drawableStart, drawables[1], drawables[2], drawables[3]);
            } else {
                tv.setCompoundDrawables(drawableStart, drawables[1], drawables[2], drawables[3]);
            }
        } else {
            tv.setCompoundDrawableTintList(tint);
        }
    }
}
